/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiwareinc.studentspring.controllers;

import com.logiwareinc.studentdetspring.hibernate.dao.HibernateUtil;
import com.logiwareinc.studentdetspring.service.RegistrationForm;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author i3
 */
@Controller
public class AddNewController {

    HibernateUtil hibernateUtil = new HibernateUtil();

    @RequestMapping(value = "/addNew.htm", method = RequestMethod.POST)
    public String addNew(@ModelAttribute("regForm") RegistrationForm regForm, ModelMap model) throws Exception {
        System.out.println(regForm.getName());
        Session session = hibernateUtil.getSession();
        Transaction txn = session.beginTransaction();
        session.save(regForm);
        txn.commit();
        model.addAttribute("name", regForm.getName());
        return "hello";
    }

    @RequestMapping(value = "/student.htm", method = RequestMethod.GET)
    public ModelAndView student() {
        return new ModelAndView("addNew", "command", new RegistrationForm());
    }
}
